package com.base.caselib.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.caselib.R;
import com.base.commonlib.utils.TimePickUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by weikailiang on 2019/10/12.
 */

public class DateCustom extends FrameLayout implements View.OnClickListener{

    private TextView mTvDate;
    private ImageView mIvDate;
    private TextView mTvName;
    private Context mContext;
    private String mRowIndex;
    private boolean isY = false;
    private SimpleDateFormat dateFormatNYRHFM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat dateFormatNYR = new SimpleDateFormat("yyyy-MM-dd");

    public DateCustom(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init(null);
    }

    public DateCustom(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    public DateCustom(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs){
        LayoutInflater.from(getContext()).inflate(R.layout.item_date, this);
        mTvDate = findViewById(R.id.tv_date);
        mIvDate = findViewById(R.id.iv_data);
        mTvName = findViewById(R.id.tv_name);
        mTvDate.setOnClickListener(this);
        mIvDate.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_date) {
            if (isY){
                TimePickUtil.pickDate(mContext,mTvDate);
            }else {
                TimePickUtil.pickDateTime(mContext, mTvDate);
            }
        }else if (i == R.id.iv_data){
            if (isY){
                TimePickUtil.pickDate(mContext,mTvDate);
            }else {
                TimePickUtil.pickDateTime(mContext, mTvDate);
            }
        }
    }
    public void setY(boolean isY){
        this.isY = isY;
    }

    public String getmTvDate(){
        if (mTvDate != null){
            return mTvDate.getText().toString().trim();
        }else {
            return "";
        }
    }
    public void setmTvDate(String data){
        if (mTvDate != null){
            mTvDate.setHint(data);
        }
    }
    public void setmTvDates(String data){
        if (mTvDate != null){
            mTvDate.setText(data);
        }
    }
    public void setmTvName(String name){
        if (mTvName != null){
            mTvName.setText(name);
        }
    }
    public void setDefaultTime(boolean isDefautl){
        if (mTvDate != null){
            if (isY){
                if (isDefautl)
                {
                    mTvDate.setText(dateFormatNYR.format(new Date()));
                }
            }else {
                if (isDefautl){
                    mTvDate.setText(dateFormatNYRHFM.format(new Date()));
                }
            }

        }
    }

    public String getmRowIndex() {
        return mRowIndex;
    }

    public void setmRowIndex(String mRowIndex) {
        this.mRowIndex = mRowIndex;
    }
}
